package com.sangcomz.fishbun.ui.picker.model;

import com.sangcomz.fishbun.adapter.image.ImageAdapter;
import com.sangcomz.fishbun.datasource.CameraDataSource;
import com.sangcomz.fishbun.datasource.FishBunDataSource;
import com.sangcomz.fishbun.datasource.ImageDataSource;
import com.sangcomz.fishbun.datasource.PickerIntentDataSource;
import com.sangcomz.fishbun.util.future.CallableFutureTask;
import ohos.utils.net.Uri;

import java.util.List;

public class PickerRepositoryImpl implements PickerRepository {

    private ImageDataSource imageDataSource;
    private FishBunDataSource fishBunDataSource;
    private PickerIntentDataSource pickerIntentDataSource;
    private CameraDataSource cameraDataSource;

    private CallableFutureTask<List<Uri>> cachedAllMediaThumbNailPath = null;

    public PickerRepositoryImpl(ImageDataSource imageDataSource, FishBunDataSource fishBunDataSource, PickerIntentDataSource pickerIntentDataSource, CameraDataSource cameraDataSource) {
        this.imageDataSource = imageDataSource;
        this.fishBunDataSource = fishBunDataSource;
        this.pickerIntentDataSource = pickerIntentDataSource;
        this.cameraDataSource = cameraDataSource;
    }

    @Override
    public CallableFutureTask<List<Uri>> getAllBucketImageUri(long bucketId, boolean clearCache) {

        if (clearCache) cachedAllMediaThumbNailPath = null;
        if (cachedAllMediaThumbNailPath == null) {
            cachedAllMediaThumbNailPath = imageDataSource.getAllBucketImageUri(bucketId, fishBunDataSource.getExceptMimeTypeList(), fishBunDataSource.getSpecifyFolderList());
        }
        return cachedAllMediaThumbNailPath;
    }

    @Override
    public CallableFutureTask<String> getDirectoryPath(long bucketId) {
        return imageDataSource.getDirectoryPath(bucketId);
    }

    @Override
    public void addAddedPath(Uri addedImage) {
        imageDataSource.addAddedPath(addedImage);
    }

    @Override
    public List<Uri> getAddedPathList() {
        return imageDataSource.getAddedPathList();
    }

    @Override
    public void addAllAddedPath(List<Uri> addedImagePathList) {
        imageDataSource.addAllAddedPath(addedImagePathList);
    }

    @Override
    public AlbumData getPickerAlbumData() {
        return pickerIntentDataSource.getAlbumData();
    }

    @Override
    public PickerViewData getPickerViewData() {
        return fishBunDataSource.getPickerViewData();
    }

    @Override
    public void setCurrentPickerImageList(List<Uri> pickerImageList) {
        fishBunDataSource.setCurrentPickerImageList(pickerImageList);
    }

    @Override
    public List<Uri> getSelectedImageList() {
        return fishBunDataSource.getSelectedImageList();
    }

    @Override
    public ImageAdapter getImageAdapter() {
        return fishBunDataSource.getImageAdapter();
    }

    @Override
    public boolean hasCameraInPickerPage() {
        return fishBunDataSource.hasCameraInPickerPage();
    }

    @Override
    public boolean useDetailView() {
        return fishBunDataSource.useDetailView();
    }

    @Override
    public boolean isLimitReached() {
        return fishBunDataSource.getMaxCount() == fishBunDataSource.getSelectedImageList().size();
    }

    @Override
    public void selectImage(Uri imageUri) {
        fishBunDataSource.selectImage(imageUri);
    }

    @Override
    public void unselectImage(Uri imageUri) {
        fishBunDataSource.unselectImage(imageUri);
    }

    @Override
    public boolean isNotSelectedImage(Uri imageUri) {
        return !fishBunDataSource.getSelectedImageList().contains(imageUri);
    }

    @Override
    public Uri getSelectedImage(int position) {
        return fishBunDataSource.getSelectedImageList().get(position);
    }

    @Override
    public int getSelectedIndex(Uri imageUri) {
        return getSelectedImageList().indexOf(imageUri);
    }

    @Override
    public Uri getPickerImage(int imagePosition) {
        return fishBunDataSource.getPickerImages().get(imagePosition);
    }

    @Override
    public String getMessageLimitReached() {
        return fishBunDataSource.getMessageLimitReached();
    }

    @Override
    public PickerMenuViewData getPickerMenuViewData() {
        return fishBunDataSource.getPickerMenuViewData();
    }

    @Override
    public int getMinCount() {
        return fishBunDataSource.getMinCount();
    }

    @Override
    public int getMaxCount() {
        return fishBunDataSource.getMinCount();
    }

    @Override
    public List<Uri> getPickerImages() {
        return fishBunDataSource.getPickerImages();
    }

    @Override
    public String getMessageNotingSelected() {
        return fishBunDataSource.getMessageNothingSelected();
    }

    @Override
    public boolean checkForFinish() {
        return fishBunDataSource.getIsAutomaticClose()
                && fishBunDataSource.getSelectedImageList().size() == fishBunDataSource.getMaxCount();
    }

    @Override
    public boolean isStartInAllView() {
        return fishBunDataSource.isStartInAllView();
    }

    @Override
    public String getDefaultSavePath() {
        //TODO 原作此处有版本判断
//        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//            cameraDataSource.getPicturePath()
//        } else {
//            cameraDataSource.getCameraPath()
//        }
        return cameraDataSource.getCameraPath();
    }
}
